#include "Player.h"

Player::Player()
{
}

Player::~Player()
{
}

void Player::Initialize(std::vector<std::vector<Node*>> &pGrid)
{
	m_xDisplayManager = DisplayManager::GetInstance();
	m_xSpriteManager = SpriteManager::GetInstance();

	m_xInputHandler = InputHandler::GetInstance();

	m_xSprite = m_xSpriteManager->LoadSprite("Images/Player.png", 5);
	m_xSprite->setOrigin(12.5f, 12.5f);

	Grid = pGrid;
	
	m_bReachedTarget = true;
	m_fCheckRange = 1.0f;
	
	m_xRange = new sf::CircleShape();
	m_xRange->setRadius(200.0f);
	m_xRange->setFillColor(sf::Color::Transparent);
	m_xRange->setOutlineThickness(1.0f);
	m_xRange->setOutlineColor(sf::Color::Red);
	m_xRange->setPosition(m_vPosition);
	m_xRange->setOrigin(200.0f, 200.0f);
	m_xDisplayManager->AddDrawable(m_xRange, 10);

	GameObject::Initialize();
}

void Player::Update(float pDeltaTime)
{
	if (m_xInputHandler->isPressed(sf::Keyboard::W))
	{
		if (m_xInputHandler->isPressed(sf::Keyboard::D))
		{
			MoveInDirection(NE);
		}
		if (m_xInputHandler->isPressed(sf::Keyboard::A))
		{
			MoveInDirection(NW);
		}
		else
		{
			MoveInDirection(North);
		}
	}

	if (m_xInputHandler->isPressed(sf::Keyboard::S))
	{
		if (m_xInputHandler->isPressed(sf::Keyboard::D))
		{
			MoveInDirection(SE);
		}
		if (m_xInputHandler->isPressed(sf::Keyboard::A))
		{
			MoveInDirection(SW);
		}
		else
		{
			MoveInDirection(South);
		}
	}

	if (m_xInputHandler->isPressed(sf::Keyboard::D))
	{
		MoveInDirection(East);
	}

	if (m_xInputHandler->isPressed(sf::Keyboard::A))
	{
		MoveInDirection(West);
	}

	if (!m_bReachedTarget)
	{
		// Check if the agent is within certain range of the target.
		if ((m_vPosition.x >= (m_vTargetPosition.x - m_fCheckRange)) && (m_vPosition.x <= (m_vTargetPosition.x + m_fCheckRange)) &&
			(m_vPosition.y >= (m_vTargetPosition.y - m_fCheckRange)) && (m_vPosition.y <= (m_vTargetPosition.y + m_fCheckRange)))
		{
			// Reached the target
			m_bReachedTarget = true;
			m_xCurrentNode = m_xTargetNode;
		}
		else
		{
			// Move to the target
			Move((m_vTargetPosition - m_vPosition) * 8.0f * pDeltaTime);
		}
	}

	m_xRange->setPosition(m_vPosition);
	GameObject::Update(pDeltaTime);
}

void Player::Cleanup()
{
	m_xDisplayManager->DeleteDrawable(m_xRange);
	GameObject::Cleanup();
}

void Player::MoveInDirection(Direction pDir)
{
	if (!m_bReachedTarget)
	{
		return;
	}

	switch (pDir)
	{
	case NE:
		if (m_xCurrentNode->m_xAdjacentNodes[4] == nullptr)
			return;
		if ((m_xCurrentNode->m_xAdjacentNodes[0]->m_bBlocked) || (m_xCurrentNode->m_xAdjacentNodes[3]->m_bBlocked))
			return;
		break;

	case SE:
		if (m_xCurrentNode->m_xAdjacentNodes[5] == nullptr)
			return;
		if ((m_xCurrentNode->m_xAdjacentNodes[3]->m_bBlocked) || (m_xCurrentNode->m_xAdjacentNodes[1]->m_bBlocked))
			return;
		break;

	case SW:
		if (m_xCurrentNode->m_xAdjacentNodes[6] == nullptr)
			return;
		if ((m_xCurrentNode->m_xAdjacentNodes[2]->m_bBlocked) || (m_xCurrentNode->m_xAdjacentNodes[1]->m_bBlocked))
			return;
		break;

	case NW:
		if (m_xCurrentNode->m_xAdjacentNodes[7] == nullptr)
			return;
		if ((m_xCurrentNode->m_xAdjacentNodes[2]->m_bBlocked) || (m_xCurrentNode->m_xAdjacentNodes[0]->m_bBlocked))
			return;
		break;
	}

	if (m_xCurrentNode->m_xAdjacentNodes[pDir] && !m_xCurrentNode->m_xAdjacentNodes[pDir]->m_bBlocked)
	{
		m_xTargetNode = m_xCurrentNode->m_xAdjacentNodes[pDir];
		m_vTargetPosition = m_xTargetNode->m_vPosition;
		m_bReachedTarget = false;
	}
}